/*
 * @copyright 2011 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of GameTimer.
 *
 * GameTimer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GameTimer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GameTimer. If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiption.gametimer;

import java.util.List;

import org.amphiption.gametimer.dao.TimerConfigurationDao;
import org.amphiption.gametimer.entity.TimerConfiguration;
import org.amphiption.gametimer.view.TimerConfigurationView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Amphiprion
 * 
 */
public class Home extends Activity {
	private TimerConfiguration current;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		buildTimerList();

	}

	private void buildTimerList() {
		List<TimerConfiguration> timers = TimerConfigurationDao.getInstance(this).getTimerConfigurations();
		LinearLayout ln = (LinearLayout) findViewById(R.id.timer_list);
		ln.removeAllViews();

		for (TimerConfiguration timer : timers) {

			TimerConfigurationView view = new TimerConfigurationView(this, timer);
			view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (v instanceof TimerConfigurationView) {
						TimerConfiguration timer = ((TimerConfigurationView) v).getTimerConfiguration();
						Intent i = new Intent(Home.this, EditTimerConfiguration.class);
						i.putExtra("TYPE", "LAUNCH");
						i.putExtra("TIMER_CONFIGURATION", timer);
						startActivity(i);
					}
				}
			});
			view.setOnLongClickListener(new View.OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					registerForContextMenu(v);
					openContextMenu(v);
					unregisterForContextMenu(v);
					return true;
				}
			});
			ln.addView(view);
		}

		if (timers.isEmpty()) {
			TextView tv = new TextView(this);
			tv.setText(R.string.empty_timer_list);
			tv.setTextSize(16);
			ln.addView(tv);
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see android.app.Activity#onPrepareOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();

		MenuItem addAccount = menu.add(0, ApplicationConstants.MENU_ID_ADD_TIMER, 1, R.string.add_timer);
		addAccount.setIcon(android.R.drawable.ic_menu_add);

		MenuItem quickStart = menu.add(0, ApplicationConstants.MENU_ID_QUICK_START, 1, R.string.quick_start);
		quickStart.setIcon(android.R.drawable.ic_menu_view);

		return true;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu,
	 *      android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		menu.clear();
		if (v instanceof TimerConfigurationView) {
			current = ((TimerConfigurationView) v).getTimerConfiguration();
			menu.add(1, ApplicationConstants.MENU_ID_EDIT_TIMER, 0, R.string.edit_timer_title);
			menu.add(1, ApplicationConstants.MENU_ID_DELETE_TIMER, 1, R.string.delete_timer_title);
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() == ApplicationConstants.MENU_ID_EDIT_TIMER) {
			edit(current);
		} else if (item.getItemId() == ApplicationConstants.MENU_ID_DELETE_TIMER) {
			TimerConfigurationDao.getInstance(this).delete(current);
			buildTimerList();
		}
		return true;
	}

	/**
	 * Edit the given timer.
	 * 
	 * @param timer
	 *            the timer to edit
	 */
	private void edit(TimerConfiguration timer) {
		Intent i = new Intent(this, EditTimerConfiguration.class);
		i.putExtra("TYPE", "EDIT");
		i.putExtra("TIMER_CONFIGURATION", timer);
		startActivityForResult(i, ApplicationConstants.ACTIVITY_RETURN_EDIT_TIMER);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == ApplicationConstants.MENU_ID_ADD_TIMER) {
			Intent i = new Intent(this, EditTimerConfiguration.class);
			i.putExtra("TYPE", "CREATE");
			startActivityForResult(i, ApplicationConstants.ACTIVITY_RETURN_CREATE_TIMER);
		} else if (item.getItemId() == ApplicationConstants.MENU_ID_QUICK_START) {
			Intent i = new Intent(this, EditTimerConfiguration.class);
			i.putExtra("TYPE", "LAUNCH");
			startActivity(i);
		}

		return true;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 *      android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == ApplicationConstants.ACTIVITY_RETURN_CREATE_TIMER) {
				TimerConfiguration timer = (TimerConfiguration) data.getExtras().get("TIMER_CONFIGURATION");
				TimerConfigurationDao.getInstance(this).create(timer);
				buildTimerList();
			} else if (requestCode == ApplicationConstants.ACTIVITY_RETURN_EDIT_TIMER) {
				TimerConfiguration timer = (TimerConfiguration) data.getExtras().get("TIMER_CONFIGURATION");
				TimerConfigurationDao.getInstance(this).update(timer);
				buildTimerList();
			}
		}
	}
}